# -*- coding: utf-8 -*-
# 测试读取配置文件内容
# python 2.7 
import os,io 

def alter(file,oldstr,newstr):
    """
    替换文件中的字符串
    : file:文件名
    : oldstr:就字符串
    : newstr:新字符串
    :return:
    """
    file_data = ""
    with io.open(file, "r", encoding="utf8") as f:
        for line in f:
            if old_str in line:
                line = line.replace(old_str,new_str)
            file_data += line

    with io.open(file,"w",encoding="utf8") as f:
        f.write(file_data)

#alter("pmuseit", ".","")


import os
def Salter(file,old_str,new_str):
    """
    将替换的字符串写到一个新的文件中，
    然后将原文件删除，新文件改为原来文件的名字
    : file: 文件路径
    : old_str: 需要替换的字符串
    : new_str: 替换的字符串
    :return: None
    """
    with io.open(file, "r", encoding="utf8") as f1,io.open("%s.bak" % file, "w", encoding="utf8") as f2:
        for line in f1:
            if old_str in line:
                line = line.replace(old_str, new_str)
            f2.write(line)
    os.remove(file)
    os.rename("%s.bak" % file, file)

#alter("pmuseit", ".","")


import re,os
def Talter(file,old_str,new_str):

    with io.open(file, "r", encoding="utf8") as f1,io.open("%s.bak" % file, "w", encoding="utf8") as f2:
        print(f1)
        print(f2)

        for line in f1:

            #print(line)
            #print("*" * 5)
            f2.write(re.sub(old_str,new_str,line))
            #f2.write(re.sub(old_str,new_str,""))

    #os.remove(file)
    #os.rename("%s.bak" % file, file)

#alter("file1", "admin", "password")

Talter("pmuseit", "\.","")

#struse="first-server-2.0.0-SNAPSHOT.jar"

#print(re.sub("\.","",struse))

#print(struse)

#<https://www.cnblogs.com/Ksen/p/6855992.html>

#<https://www.cnblogs.com/wc-chan/p/8085452.html>
